package com.cn.mmd.controller;

/**
 * Created by Administrator on 2016/12/22.
 */


import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.springframework.stereotype.Controller;

@Controller
public class JPushController {



    // demo App defined in resources/jpush-api.conf

    public static final String TITLE = "孜点";
    public static String ALERT ="";
    public static final String MSG_CONTENT = "孜点祝新老客户新春快乐";
//    public static final String REGISTRATION_ID = "0900e8d85ef";//id
    public static String TAG = null;//标签

    public static JPushClient jpushClient = null;

    public static void testSendPush(String s,String tag) {
        ALERT = s;
        TAG = tag;
        String appKey = "9edfaa64d1fc35e6493c212a";
        String masterSecret = "566ff083db166e1f94a3d3ce";

        jpushClient = new JPushClient(masterSecret, appKey, 30);

        // HttpProxy proxy = new HttpProxy("localhost", 3128);
        // Can use this https proxy: https://github.com/Exa-Networks/exaproxy


        // For push, all you need do is to build PushPayload object.
        //PushPayload payload = buildPushObject_all_all_alert();
        //全部
        PushPayload payload = buildPushObject_all_alias_alert1();
        PushResult result = null;
        try {
            result = jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            e.printStackTrace();
            System.out.print(e);
        } catch (APIRequestException e) {
            e.printStackTrace();
            System.out.print(e);
        }

    }

    public static void testSendPush1(String s,String alias) {//这个可以设置标签
        ALERT = s;
//        TAG = tag;
        String appKey = "9edfaa64d1fc35e6493c212a";
        String masterSecret = "566ff083db166e1f94a3d3ce";

        jpushClient = new JPushClient(masterSecret, appKey, 172800);

        // HttpProxy proxy = new HttpProxy("localhost", 3128);
        // Can use this https proxy: https://github.com/Exa-Networks/exaproxy


        // For push, all you need do is to build PushPayload object.
        //PushPayload payload = buildPushObject_all_all_alert();
        //全部
        PushPayload payload = buildPushObject_all_alias_alert2(alias);
        PushResult result = null;
        try {
            result = jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {
//            e.printStackTrace();
//            System.out.print(e);
        } catch (APIRequestException e) {
//            e.printStackTrace();
//            System.out.print(e);
        }

    }

//    public static PushPayload buildPushObject_all_all_alert() {
//        return PushPayload.alertAll(ALERT);
//    }


    public static PushPayload buildPushObject_all_alias_alert() {//这是抽奖
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//设置接受的平台
                .setAudience(Audience.all())//Audience设置为all，说明采用广播方式推送，所有用户都可以接收到
                .setNotification(Notification.alert(ALERT))
                .build();
    }

    public static PushPayload buildPushObject_all_alias_alert1() {//这是标签评价的，标签推送
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//设置接受的平台
                .setAudience(Audience.tag_and(TAG))//通过标签区分推送的对象
                .setNotification(Notification.alert(ALERT))
                .build();
    }
    public static PushPayload buildPushObject_all_alias_alert2(String alias) {//这是标签评价的，标签推送
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//设置接受的平台
                .setAudience(Audience.alias(alias))//通过标签区分推送的对象
                .setNotification(Notification.alert(ALERT))
                .build();
    }

////    public static PushPayload buildPushObject_android_tag_alertWithTitle() {
////        return PushPayload.newBuilder()
////                .setPlatform(Platform.android())
////                .setAudience(Audience.all())
////                .setNotification(Notification.android(ALERT, TITLE, null))
////                .build();
//    }

//    public static PushPayload buildPushObject_android_and_ios() {
//        return PushPayload.newBuilder()
//                .setPlatform(Platform.android_ios())
//                .setAudience(Audience.tag("tag1"))
//                .setNotification(Notification.newBuilder()
//                        .setAlert("alert content")
//                        .addPlatformNotification(AndroidNotification.newBuilder()
//                                .setTitle("Android Title").build())
//                        .addPlatformNotification(IosNotification.newBuilder()
//                                .incrBadge(1)
//                                .addExtra("extra_key", "extra_value").build())
//                        .build())
//                .build();
//    }
//
//    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
//        return PushPayload.newBuilder()
//                .setPlatform(Platform.ios())
//                .setAudience(Audience.tag_and("tag1", "tag_all"))
//                .setNotification(Notification.newBuilder()
//                        .addPlatformNotification(IosNotification.newBuilder()
//                                .setAlert(ALERT)
//                                .setBadge(5)
//                                .setSound("happy")
//                                .addExtra("from", "JPush")
//                                .build())
//                        .build())
//                .setMessage(Message.content(MSG_CONTENT))
//                .setOptions(Options.newBuilder()
//                        .setApnsProduction(true)
//                        .build())
//                .build();
//    }
//
//    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
//        return PushPayload.newBuilder()
//                .setPlatform(Platform.android_ios())
//                .setAudience(Audience.newBuilder()
//                        .addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
//                        .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
//                        .build())
//                .setMessage(Message.newBuilder()
//                        .setMsgContent(MSG_CONTENT)
//                        .addExtra("from", "JPush")
//                        .build())
//                .build();
//    }
}
